<?php
if ($model->widget_id)
{
    $widget = $model->widget;
    $this->breadcrumbs=array(
        'Widgets'=>array('/Cms/admin/widget/admin'),
        $widget->name=>array('/Cms/admin/widget/update', 'id'=>$model->widget_id),
        'Manage Instances',
    );
    echo "<h1>Manage Instances of {$widget->name}</h1>";    
}
else
{
    $this->breadcrumbs=array(
        'Widgets'=>array('/Cms/admin/widget/admin'),
        'Widget instances'=>array('index'),
        'Manage',
    );
    echo '<h2>Manage Widget Instances</h2>';
}

$this->menu=array(
	array('label'=>'Create Instance', 'url'=>array('/Cms/admin/widget/updateInstance', 'id'=>$model->widget_id, 'instanceId'=>0)),
    array('label'=>'Activate selected items', 'url'=>array("/Core/service/ajax", "SID"=>"Cms.widgetData.changeStatus", "value"=>1), "linkOptions"=>array('class'=>'change-status')),
    array('label'=>'Deactivate selected items', 'url'=>array("/Core/service/ajax", "SID"=>"Cms.widgetData.changeStatus", "value"=>0), "linkOptions"=>array('class'=>'change-status')),
);
$script = "
jQuery('#sidebar .change-status').live('click', function(){
    var data = [];
    
    jQuery.each(jQuery('#widget-data-grid input.select-on-check:checked'), function(){
        data.push(jQuery(this).val());
    });
    
    if (data.length <= 0)
    {
        alert('Please select at least one item');
        return false;
    }

    jQuery.get(jQuery(this).attr('href'), {'ids' : data}, function(){
        jQuery.fn.yiiGridView.update('widget-data-grid');
    });
    return false;
});
jQuery('.grid-view .status-column a').live('click', function(){
    jQuery.get(jQuery(this).attr('href'), function(){
        jQuery.fn.yiiGridView.update('widget-data-grid');
    });
    return false;
});
";
Yii::app()->clientScript->registerScript(__CLASS__.'#ChangeStatus', $script, CClientScript::POS_READY);

if ($model->widget_id)
{
    $script = "
    var widget_filter = jQuery('<input />').attr({
        'type' : 'hidden',
        'name' : 'WidgetData[widget_id]',
        'value' : ".(int) $model->widget_id.",
    });
    jQuery('#widget-data-grid .filters td:first').append(widget_filter);
    ";
    Yii::app()->clientScript->registerScript(__CLASS__.'#DefaultFilterByWidgetId', $script, CClientScript::POS_READY);
}
?>

<?php $grid = $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'widget-data-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
    'selectableRows'=>2,
    'selectionChanged'=>"updateSelectors",
    'afterAjaxUpdate'=>"js:function(){jQuery('#widget-data-grid .filters td:first').append(widget_filter);}",
	'columns'=>array(
        array(
            'class'=>'CCheckBoxColumn',
            'value'=>'$data->id',
            'htmlOptions'=>array('width'=>'3%'),
        ),
		array(
            'type'=>'html',
            'name'=>'name',
            'value'=>'CHtml::link($data->name.($data->type == WidgetDefinition::TYPE_STATIC ? " (".$data->container.")" : ""), array("/Cms/admin/widget/updateInstance","id" => $data->widget_id, "instanceId" => $data->id))',
        ),
        array(
            'name'=>'creation_datetime',
            'value'=>'Yii::app()->getDateFormatter()->formatDateTime(strtotime($data->creation_datetime))',
            'filter'=>false,
            'htmlOptions'=>array(
                'class'=>'datetime-column'
            )
        ),
        array(
            'name'=>'last_update',
            'value'=>'Yii::app()->getDateFormatter()->formatDateTime(strtotime($data->last_update))',
            'filter'=>false,
            'htmlOptions'=>array(
                'class'=>'datetime-column'
            )
        ),
        array(
            'name'=>'status',
            'type'=>'raw',
            'value'=>'CHtml::link($data->statusText, array("/Core/service/ajax", "SID"=>"Cms.widgetData.changeStatus", "ids[]"=>$data->id, "value"=>$data->status ? 0 : 1), array("class"=>($data->status ? "active" : "")))',
            'filter'=>false,
            'htmlOptions'=>array(
                'class'=>'status-column'
            )
        ),
		array(
			'class'=>'CButtonColumn',
            'updateButtonUrl' => 'Yii::app()->urlManager->createUrl("/Cms/admin/widget/updateInstance",array("id" => $data->widget_id, "instanceId" => $data->id))',
            'template'=> '{update} {delete}'
		),
	),
)); 

if ($grid->dataProvider->ItemCount) {
    $this->menu[] = array('label' => 'Delete selected items', 'url'=>$this->createUrl('delete'), 'linkOptions' => array('onclick'=>'return multipleDelete("widget-data-grid",this.href)'));
}
Yii::app()->clientScript->registerScriptFile(Yii::app()->core->AssetUrl.'/scripts/gridview.js', CClientScript::POS_BEGIN);
?>